package com.turing.post.cardmanagement.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.cardmanagement.util.CardFace;
import com.turing.post.cardmanagement.util.CardFaceBusiness;
import com.turing.post.cardmanagement.util.CardFaceDao;
import com.turing.post.cardmanagement.util.CardManaRespCode;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 11, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.CardFaceBusinessImpl")
@Remote
@Local
public class CardFaceBusinessImpl implements CardFaceBusiness {

	@EJB(mappedName = "cardmanagement.impl.CardFaceDaoImpl")
	private CardFaceDao cardFaceDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public void insertCardFace(PCPSWebInput input, CardFace cardFace)
			throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "登录用户归属机构为空!");
		}
		if (PCPSUtil.isEmpty(cardFace.getCardFaceName())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "必输参数卡图样名称为空!");
		}
		if (PCPSUtil.isEmpty(cardFace.getMediaType())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "必输参数介质类型为空!");
		}
		String issueCompanyNo =PCPSUtil.getIssueCompanyNo(input);
		cardFace.setCardFaceId(pcpsCommonDaoImpl.getUniqueIntegerKey());
		cardFace.setIssueCompanyNo(issueCompanyNo);
		cardFace.setAllowMultiApp(CardFace.CARDFACE_AllowMultiApp_NoAllow);// 默认CPU多界面卡不允许多应用
		cardFaceDaoImpl.insert(cardFace);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Add,
				"新增卡名称", "CardFaceName:" + cardFace.getCardFaceName());
	}

	@Override
	public void updateCardFace(PCPSWebInput input, CardFace cardFace)
			throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "登录用户归属机构为空!");
		}
		if (PCPSUtil.isEmpty(cardFace.getCardFaceName())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "必输参数卡图样名称为空!");
		}
		if (PCPSUtil.isEmpty(cardFace.getMediaType())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "必输参数介质类型为空!");
		}
		if (cardFace.getCardFaceId() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "待修改卡图样编号为空!");
		}
		CardFace _cardFace = cardFaceDaoImpl.getCardFaceByCardFaceId(cardFace
				.getCardFaceId());
		if (_cardFace == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "待修改卡图样系统中不存在!");
		}
		_cardFace.setCardFaceName(cardFace.getCardFaceName());

		if (cardFace.getImageId() != null)
			_cardFace.setImageId(cardFace.getImageId());

		if (cardFace.getBackImageId() != null)
			_cardFace.setBackImageId(cardFace.getBackImageId());

		_cardFace.setMediaType(cardFace.getMediaType());
		_cardFace.setRemark(cardFace.getRemark());
		cardFaceDaoImpl.update(_cardFace);

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"修改卡面图样", "CardFaceName:" + cardFace.getCardFaceName());
	}
}
